{% extends 'index.html' %}
{% block page-content %}
<div id="page-wrapper">
    <div class="row">
         <div class="col-lg-12">
              <h1 class="page-header"><i class="fa  fa-wrench"></i><code>{{user_info.username}}</code> 资料配置</h1>
         </div>
                <!-- /.col-lg-12 -->
    </div>
	<div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                           	<i class="fa   fa-gear"></i> 用户管理
                        </div>
                        <div class="panel-body">

				                            
				           <ul class="nav nav-tabs">
				              <li class="active"><a href="#home" data-toggle="tab"><i class="fa   fa-user"></i> 权限</a>
				              </li>
				              <!-- <li><a href="#profile" data-toggle="tab"><i class="fa    fa-linux"></i> 主机</a> -->
				              </li>
				              {% if user_info.id !=  request.user.id and request.user.is_superuser %}
							  <li><a href="#cpassword" data-toggle="tab"><i class="fa   fa-lock"></i> 重置密码</a>
				              </li>     
				              {% endif %}         
				           </ul>
				
				                            
				           <div class="tab-content">
				              <div class="tab-pane fade in active" id="home">
				              			<br>	
										<div id="asset_net_chioce" class="col-lg-6">
		
											<form role="form" method="post" id="addHost" class="main form-horizontal" >{% csrf_token %}			
						
												<div class="form-group" >
													 <label class="col-sm-2 control-label"><i class="fa fa-user"></i> 用户名</label>
													 <div class="col-sm-6">
													 	<input class="form-control" type="text" value="{{user_info.username}}" placeholder="用户名" name="username" />
													 </div>
												</div>
												
												<div class="form-group" >
													 <label class="col-sm-2 control-label"><i class="fa fa-envelope"></i> 邮箱</label>
													 <div class="col-sm-6">
													 	<input class="form-control" type="text" id="form-field-3" value="{{user_info.email}}" placeholder="email" name="email" />
													 </div>
												</div>				
												
												<div class="form-group">													 
													{% if user_info.is_superuser %}
														<label class="col-sm-2 control-label"><i class="fa  fa-check"></i> 管理员</label>													
													{% else %}
														<label class="col-sm-2 control-label"><i class="fa fa-times"></i> 管理员</label>
													{% endif %}
													 <div class="col-sm-6">
														<select class="form-control"  name="is_superuser">
															{% if user_info.is_superuser == 1 %}
																<option selected="selected" value="1">是</option>
																<option value="0">否</option>																			
															{% else %}
																<option  value="1">是</option>
																<option selected="selected" value="0">否</option>
															{% endif %}																		
														</select>
													 </div>
												</div>							
												
						
												
												<div class="form-group">
													 <label class="col-sm-2 control-label"><i class="fa fa-group"></i> 用户分组</label>
													 <div class="col-sm-6">
														<select multiple class="selectpicker" name="groups" data-live-search="true">	
															{% for ds in groupList %}
																{% if ds.status == 1  %}			
																<option selected="selected" value="{{ds.id}}">{{ds.name}}</option>
																{% else %}
																<option value="{{ds.id}}">{{ds.name}}</option>
																{% endif %}
																				
															{% endfor %}
														</select>
													 </div>
												</div>	
												
												
												<div class="form-group">
													 <label class="col-sm-2 control-label">分配权限</label>
													 <div class="col-sm-6">
														<select multiple class="selectpicker" name="perms" data-live-search="true" data-live-search-placeholder="Search" data-actions-box="true">	
															{% for ds in permList %}
																{% if ds.status == 1 %}
																	<option selected="selected" value="{{ds.id}}">{{ds.name}}</option>
																	{% else %}
																	<option value="{{ds.id}}">{{ds.name}}</option>
																{% endif %}	
															{% endfor %}
														</select>	
													 </div>
												</div>	

												<div class="form-group">
													 <label class="col-sm-2 control-label"><i class="fa fa-terminal"></i> 激活状态</label>
													 <div class="col-sm-6">
														<select class="form-control" name="is_active">
															{% if user_info.is_active == 0 %}
																<option selected="selected" value="0">未激活</option>
																<option value="1">已激活</option>																			
															{% else %}
																<option  value="0">未激活</option>
																<option selected="selected" value="1">已激活</option>
															{% endif %}																		
														</select>
													 </div>
												</div>																								
												
												<div class="hr hr32 hr-dotted"></div>                                        
												<div class="form-group">
													<div class="col-md-offset-3 col-md-9">
														<button class="btn btn-default" type="submit" >
															<i class="fa fa-check"></i>
																			提交
														</button>						
																		&nbsp; &nbsp; &nbsp;
														<button class="btn btn-default" type="reset">
															<i class="fa fa-undo"></i>
																			撤销
														</button>
													</div>
												</div>
												</form>
											</div>
				              </div>
				              <div class="tab-pane fade" id="profile">
				                   <div class="row">
				                   		<div class="col-lg-4">
				                   			<br>
		                                    <form role="form" id="websshModelRun" class="main form-horizontal" >{% csrf_token %}
												<fieldset>
													<div class="form-group" >			 		             
															<label class="col-sm-2 control-label">选择主机</label>	
															<div class="col-sm-6">
																<select class="form-control" name="server_model" id="server_model" onchange="javascript:oBtwebsshServerl();">
																	<option value="" name="server_model">选择一个类型进行分配</option>	
																	<option value="service" name="server_model">项目类型</option>	
																	<option value="group" name="server_model">使用部门</option>	
																	<option value="custom" name="server_model">自定义</option> 	
																</select>		
															</div>	
													</div>	
													<div id='project_server'  style="display:none;">
														<div class="form-group">
															 <label class="col-sm-2 control-label" >所属项目</label>
															 <div class="col-sm-6">
					                                            <select class="form-control" name="webssh_project"  id="webssh_project" onchange="javascript:oBtProjectSelect();" required>
					                                            	<option name="project" value="" selected="selected">请选择一个项目</option>
					                                            	{% for ds in projectList %}
					                                            		<option name="project" value="{{ds.id}}">{{ds.project_name}}</option>
					                                            	{% endfor %}
					                                            </select>
															 </div>
														</div>										
														<div class="form-group">
															 <label class="col-sm-2 control-label" >业务类型</label>
															 <div class="col-sm-6">
					                                            <select class="form-control" name="webssh_service"  id="webssh_service" required>
					                                            	{% for ds in serviceList %}
					                                            		<option name="service" value="{{ds.id}}">{{ds.service_name}}</option>
					                                            	{% endfor %}
					                                            </select>
															 </div>
														</div>	
													</div>												
													<div id="group_server" class="form-group" style="display:none;">
														 <label class="col-sm-2 control-label" >使用部门</label>
														 <div class="col-sm-6">
				                                            <select class="form-control" name="webssh_group"  id="webssh_group" required>
				                                            	{% for ds in groupList %}
				                                            		<option name="group" value="{{ds.id}}">{{ds.name}}</option>
				                                            	{% endfor %}
				                                            </select>
														 </div>
													</div>																																				
													<div id="custom_server" class="form-group" style="display:none;">
														 <label class="col-sm-2 control-label" >自定义</label>
														 <div class="col-sm-6">
				                                            <select  multiple class="selectpicker" name="webssh_server" data-live-search="true" data-live-search-placeholder="Search" data-actions-box="true" required>
				                                            	{% for ds in serverList %}
				                                            		{% if ds.status == 1 %}
				                                            			<option name="server" value="{{ds.id}}" selected="selected">{{ds.ip}}</option>
				                                            		{% else %}
				                                            			<option name="server" value="{{ds.id}}">{{ds.ip}}</option>
				                                            		{% endif %}
				                                            	{% endfor %}
				                                            </select>
														 </div>
													</div>
								
													<div class="form-group">
														<label class="col-sm-3 control-label"></label>
														<button type="button"  class="btn btn-default" >撤销</button>
												 		<button type="button"  class="btn btn-default" onclick="runWebsshModel(this)" >提交</button>
											 		</div>																															 		
												</form>  
				                   		</div>
				              		</div>
				              	</div>
				              {% if user_info.id !=  request.user.id and request.user.is_superuser %}	
				              <div class="tab-pane fade" id="cpassword">
				                   <div class="row">
				                   		<div class="col-sm-2"></div>
				                   		<div class="col-sm-8">
				                   			<br>
											<form role="form" method="post" id="modfPasswd" class="main form-horizontal" >{% csrf_token %}			
												<div class="form-group">
													<label class="col-sm-2 control-label"><i class="fa fa-lock"></i></label>
													<div class="col-sm-4">
														<input type="password" class="form-control" placeholder="Password" id="password" name="password" />
													</div>
												</div>
												<div class="form-group">
													<label class="col-sm-2 control-label"><i class="fa fa-retweet"></i></label>
													<div class="col-sm-4">
														<input type="password" class="form-control" placeholder="Repeat password" id="c_password" name="c_password"/>
													</div>
												</div>																																											
												
												<div class="hr hr32 hr-dotted"></div>                                        
												<div class="form-group">
													<div class="col-md-offset-3 col-md-9">
														<button class="btn btn-default" type="buttun" onclick="modfUserPasswd(this)">
															<i class="fa fa-check"></i>
																			提交
														</button>						
																		&nbsp; &nbsp; &nbsp;
														<button class="btn btn-default" type="reset">
															<i class="fa fa-undo"></i>
																			撤销
														</button>
													</div>
												</div>
											</form>				                   			
										</div>

				                   </div>
				              </div>
				              {% endif %}				              	
				           </div>


                            <!-- /.row (nested) -->
                        </div>
                            <!-- /.row (nested) -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>    
</div>




	  
<script type="text/javascript">
{% if errorInfo %}
	window.wxc.xcConfirm("{{errorInfo}}", window.wxc.xcConfirm.typeEnum.error);
{% endif %}

	function modfUserPasswd(obj) {
		var btnObj = $(obj);
		btnObj.attr('disabled',true);
 		var post_data = {}; 
		var form = document.getElementById('modfPasswd');
		var required = ["c_password","password"];
		for (var i = 1; i < form.length; ++i) {
			var name = form[i].name;
			var value = form[i].value;
			idx = $.inArray(name, required);
			if (idx >= 0  && value.length == 0){
				window.wxc.xcConfirm("密码不能为空", window.wxc.xcConfirm.typeEnum.error);
				return false;
			}
			else{
				post_data[name] = value;
			};
			
		};
		$.ajax({
			dataType: "JSON",
			url:'/user/{{user_info.id}}/', //请求地址
			type:"PUT",  //提交类似
			data:post_data, //提交参数
			success:function(response){
				btnObj.removeAttr('disabled');
				if (response["code"] == "500"){
					btnObj.removeAttr('disabled');
					window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
				}
				else {
					btnObj.removeAttr('disabled');
					window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.success);					
				}
			},
	    	error:function(response){
	    		btnObj.removeAttr('disabled');
	    		window.wxc.xcConfirm("密码修改失败", window.wxc.xcConfirm.typeEnum.error);
	//	    		location.reload();
	    	}
		})	
	}

	function oBtwebsshServerl(){
		   var obj = document.getElementById("server_model"); 
		   var index = obj.selectedIndex;
		   var value = obj.options[index].value; 
		   if (value=="group"){
			   document.getElementById("group_server").style.display = "";
			   document.getElementById("custom_server").style.display = "none";
			   document.getElementById("project_server").style.display = "none";
		   }
		   else if (value=="custom"){
			   document.getElementById("group_server").style.display = "none";
			   document.getElementById("custom_server").style.display = "";
			   document.getElementById("project_server").style.display = "none";
		   }
		   else if (value=="service"){
			   document.getElementById("group_server").style.display = "none";
			   document.getElementById("custom_server").style.display = "none";
			   document.getElementById("project_server").style.display = "";
		   }	   
		   else {
			   document.getElementById("group_server").style.display = "none";
			   document.getElementById("custom_server").style.display = "none"; 
			   document.getElementById("project_server").style.display = "none";
		   }	
	}
	
	function oBtProjectSelect(){
		   $('#webssh_service').removeAttr("disabled");
		   var obj = document.getElementById("webssh_project"); 
		   var index = obj.selectedIndex;
		   var projectId = obj.options[index].value; 
		   if ( projectId > 0){	 
				$.ajax({
					dataType: "JSON",
					url:'/api/project/'+ projectId + '/', //请求地址
					type:"GET",  //提交类似
					success:function(response){
						var binlogHtml = '<select class="form-control" name="webssh_service" id="webssh_service" onchange="javascript:oBtServiceSelect();" required><option selected="selected" name="webssh_service" value="">请选择业务类型</option>'
						var selectHtml = '';
						for (var i=0; i <response["service_assets"].length; i++){
							 selectHtml += '<option name="webssh_service" value="'+ response["service_assets"][i]["id"] +'">' + response["service_assets"][i]["service_name"] + '</option>' 
						};                        
						binlogHtml =  binlogHtml + selectHtml + '</select>';
						document.getElementById("webssh_service").innerHTML= binlogHtml;	
							
					},
				});	
		   }
		   else{
			   $('#webssh_service').attr("disabled",true);
		   }
	
	}


	function runWebsshModel(obj) {
		var btnObj = $(obj);
		btnObj.attr('disabled',true);
		$.ajax({
			url:'/user/server/{{user_info.id}}/', //请求地址
			type:"POST",  //提交类似
			data:$('#websshModelRun').serialize(),  //提交参数
			success:function(response){
				btnObj.removeAttr('disabled');
				if (response["code"] == "500"){
					btnObj.removeAttr('disabled');
					window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
				}
				else {
					btnObj.removeAttr('disabled');
					window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.success);					
				}
				
			},
	    	error:function(response){
	    		btnObj.removeAttr('disabled');
	    		window.wxc.xcConfirm("添加失败", window.wxc.xcConfirm.typeEnum.error);
	    	}
		})	
	}
</script>

{% endblock %}